package cn.wh.ymcc.service.impl;

import cn.wh.ymcc.domain.MessageSms;
import cn.wh.ymcc.mapper.MessageSmsMapper;
import cn.wh.ymcc.service.IMessageSmsService;
import cn.wh.ymcc.util.AssertUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author nidie.wang
 * @since 2022-07-15
 */
@Service
public class MessageSmsServiceImpl extends ServiceImpl<MessageSmsMapper, MessageSms> implements IMessageSmsService {


    @Override
    public void pullBlack(MessageSms messageSms) {
        EntityWrapper<MessageSms> tWrapper = new EntityWrapper<>();
        tWrapper.eq("phone",messageSms.getPhone());  // where phone = 123123
        List<MessageSms> message = selectList(tWrapper);
        AssertUtil.isNotNull(message,"没查到电话号码哦");
        for (MessageSms sms : message) {
            Integer state = sms.getState();
            AssertUtil.isTrue(state!=-1,"已经拉黑");
            sms.setState(-1);
            updateById(sms);
        }
    }

    @Override
    public void removeBlock(MessageSms sms) {
        Wrapper<MessageSms> tWrapper = new EntityWrapper<>();
        tWrapper.eq("phone",sms.getPhone());  // where phone = 123123
        List<MessageSms> message = selectList(tWrapper);
        AssertUtil.isNotNull(message,"没查到电话号码哦");
        for (MessageSms sm : message) {
            Integer state = sm.getState();
            AssertUtil.isTrue(state==-1,"没有被拉黑");
            sm.setState(0);
            updateById(sm);
        }
    }
}
